//
//  RootViewController.m
//  第三组_阅读器
//
//  Created by My Mac on 2016/12/23.
//  Copyright © 2016年 My Mac. All rights reserved.
//

#import "RootViewController.h"
#import "BookShelfCell.h"
#import "ReadViewController.h"
#define Width self.view.bounds.size.width
#define Height self.view.bounds.size.height
@interface RootViewController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation RootViewController
static int number = 0;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self initViews];
    [self setData];
}
-(void)setData
{
    data = [NSMutableArray new];
    for (int i=1; i<=18; i++) {
        [data addObject:[NSString stringWithFormat:@"%02d",i]];
    }
}
-(void)initViews
{
    mainScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Width, Height )];
    mainScrollView.contentSize = CGSizeMake(2*Width, Height-113);
    mainScrollView.bounces = NO;
    mainScrollView.backgroundColor = [UIColor whiteColor];
    mainScrollView.showsVerticalScrollIndicator = NO;
    mainScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:mainScrollView];
    
    for (int i=0; i<2; i++) {
        _tableView[i] = [[UITableView alloc]initWithFrame:CGRectMake(0+i*Width, 0, Width, mainScrollView.contentSize.height) style:UITableViewStylePlain];
        _tableView[i].dataSource = self;
        _tableView[i].delegate = self;
        _tableView[i].tag = i;
        _tableView[i].bounces = NO;
        [mainScrollView addSubview:_tableView[i]];
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * ID = @"cell";
    BookShelfCell * cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if(cell == nil){
        cell = [[BookShelfCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    for (int i=0; i<2; i++) {
        if(tableView.tag == i){
            [self setbuttonToCell:cell];
        }
    }
    return cell;
}
-(void)setbuttonToCell:(UITableViewCell *)cell
{
    CGFloat btnW = (Width-4*15)/3;
    CGFloat btnH = mainScrollView.contentSize.height/3.0 - 30;
    for (int i=0; i<3; i++) {
        button = [[UIButton alloc]initWithFrame:CGRectMake((i+1)*15+i*btnW, 15, btnW, btnH)];
        button.tag = number;
        [button setImage:[UIImage imageNamed:data[number]] forState:UIControlStateNormal];
        number++;
        [cell addSubview:button];
        [button addTarget:self action:@selector(readBook:) forControlEvents:UIControlEventTouchUpInside];
    }
}
-(void)readBook:(UIButton *)sender
{
    NSArray * bookName = @[@"爱在仙境的日子",@"那时长大",@"不必知道我是谁",@"当你孤单你会想起谁",@"蝴蝶来过这世界",@"ID",@"沙漏",@"十年",@"糖衣",@"调频",@"我不是你的冤家",@"我要我们在一起",@"校园里的裙摆",@"小妖",@"最熟悉的陌生人",@"左耳",@"左耳",@"未知"];
    NSInteger bookTag = sender.tag;
    NSLog(@"这是第%d本书",bookTag+1);
    
    ReadViewController * readView = [[ReadViewController alloc]init];
    readView.navigationItem.title = bookName[bookTag];
    NSString * fileName = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",bookTag+1] ofType:@"txt"];
    readView.bookStr = [NSString stringWithContentsOfFile:fileName encoding:NSUTF8StringEncoding error:nil];
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:readView];
    [self.navigationController presentViewController:nav animated:YES completion:nil];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return mainScrollView.contentSize.height/3.0;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
